package fudan.se.lab2.controller;


import fudan.se.lab2.controller.request.GetPaperRequest;
import fudan.se.lab2.controller.request.UploadRequest;
import fudan.se.lab2.domain.Contribution;
import fudan.se.lab2.service.ContributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class ContributeController {
    private final ContributeService contributeService;

    @Autowired
    public ContributeController(ContributeService contributeService) {
        this.contributeService = contributeService;
    }

    @PostMapping("/uploadWork")
    public ResponseEntity<String> uploadWork(UploadRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        request.setFileName(file.getOriginalFilename());
        String path = contributeService.uploadWork(request);
        File dest = new File(path);
        file.transferTo(dest);
        return ResponseEntity.ok(path);
    }

    @PostMapping("/getMeetingTopics")
    public ResponseEntity<List<String>> getMeetingTopics(@RequestBody String meetingFullName) {
        return ResponseEntity.ok(contributeService.getMeetingTopics(meetingFullName));
    }

    @PostMapping("/getPaperInfo")
    public ResponseEntity<Contribution> getPaperInfo(@RequestBody GetPaperRequest getPaperRequest) {
        return ResponseEntity.ok(contributeService.getPaperInfo(getPaperRequest));
    }

    @PostMapping("/updateWork")
    public ResponseEntity<String> updateWork(UploadRequest request, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        request.setFileName(file.getOriginalFilename());
        String path = contributeService.updateWork(request);
        File dest = new File(path);
        file.transferTo(dest);
        return ResponseEntity.ok(path);
    }
}
